package com.app.juyanresturantapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.app.juyanresturantapp.adapter.MyAdapter;
import com.app.juyanresturantapp.bean.FoodsBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ProductListActivity extends AppCompatActivity {
String TAG = "ProductListActivity";
ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        initView();
        getData();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String i = String.valueOf(position+1);
        Intent intent = new Intent(ProductListActivity.this, ProductDetailActivity.class);
        // 设置你要传的参数
        intent.putExtra("id", i);
        startActivity(intent);
    }
});



    }

    private void initView() {
        listView = findViewById(R.id.listView);
    }

    private void getData() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url(getResources().getString(R.string.url)+"/app13/food/list")
                .post(body)
                .build();

        final Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                FoodsBean foodsBean = gson.fromJson(data, FoodsBean.class);
                if (foodsBean.isSuccess()) {
                    // 获取链表数据
                    List list = foodsBean.getList();
                    //使用runOnUITHread才能获取到
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 构建适配器对象
                            MyAdapter adapter = new MyAdapter(getApplicationContext(),list);
                            // 为listview设置适配器对象
                            listView.setAdapter(adapter);
                        }
                    });


                } else {
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }



            }
        });

    }
}